package com.wangdao.mall.controller.admin;

import com.google.gson.Gson;
import com.wangdao.mall.model.Result;
import com.wangdao.mall.model.User;
import com.wangdao.mall.service.UserService;
import com.wangdao.mall.service.UserServiceImp;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @Author:HaiyangTian
 * @Description:
 * @Date: Created in 16:43 2020/4/30
 * @Modified By:
 */
@WebServlet("/api/admin/user/*")
public class UserServlet extends HttpServlet {

    private UserService userService = new UserServiceImp();

    Gson gson = new Gson();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String uri = request.getRequestURI();
        String action = uri.replace("/api/admin/user/", "");

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String uri = request.getRequestURI();
        String action = uri.replace("/api/admin/user/", "");
        if ("allUser".equals(action)){
            allUser(request,response);
        }else if ("deleteUser".equals(action)){
            deleteUser(request,response);
        }else if ("searchUser".equals(action)){
            searchUser(request,response);
        }
    }

    /**
     * 搜索指定用户
     * @param request
     * @param response
     */
    private void searchUser(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String nickname = request.getParameter("word");
        Result result = new Result();
        if ("".equals(nickname)){
            result.setCode(10000);
            result.setMessage("搜索内容不能为空");
            response.getWriter().println(gson.toJson(result));
            return;
        }
        List<User> users = userService.searchUser(nickname);
        result.setCode(0);
        result.setData(users);
        response.getWriter().println(gson.toJson(result));
    }

    /**
     * 删除指定的用户信息
     * @param request
     * @param response
     */
    private void deleteUser(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String id = request.getParameter("id");
        int row = userService.deleteUser(id);
        Result result = new Result();
        if (row == 1){
            result.setCode(0);
        }else{
            result.setCode(10000);
            result.setMessage("删除失败");
        }
        response.getWriter().println(gson.toJson(result));
    }

    /**
     * 显示所有可以管理的用户的信息
     * @param request
     * @param response
     * @throws IOException
     */
    private void allUser(HttpServletRequest request, HttpServletResponse response) throws IOException {
        List<User> users = userService.allUser();
        Result result = new Result();
        if (users.isEmpty()){
            result.setCode(10000);
            result.setMessage("当前没有任何用户可以管理");
        }else{
            result.setCode(0);
            result.setData(users);
        }
        response.getWriter().println(gson.toJson(result));
    }
}
